/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mp3tagger;

import entagged.audioformats.exceptions.CannotReadException;
import java.io.File;
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import javax.swing.AbstractListModel;

/**
 *
 * @author Rajab
 */
public class FileListModel extends AbstractListModel {

    private ArrayList<ListItem> data = new ArrayList<ListItem>();
    private ExecutorService es = Executors.newSingleThreadExecutor();

    public FileListModel(File[] files) {
        for (File file : files) {
            data.add(new ListItem(file));
        }
        fireIntervalAdded(this, 0, data.size() - 1);
    }

    public FileListModel() {
    }

    public int getSize() {
        return data.size();
    }

    public Object getElementAt(int index) {
        return data.get(index);

    }

    public void add(File item) {
        data.add(new ListItem(item));
        fireIntervalAdded(this, 0, data.size() - 1);
    }

    public void add(File[] files) {
        for (File file : files) {
            data.add(new ListItem(file));
        }
        fireIntervalAdded(this, 0, data.size() - 1);
    }

    private class ReadThread extends Thread {

        @Override
        public void run() {
            for (ListItem item : data) {
                if (!item.isRead()) {
                    try {
                        item.read();
                    } catch (CannotReadException ex) {
                        System.out.println(ex);
                    }
                }

            }
            FileListModel.this.fireContentsChanged(FileListModel.this, 0, data.size() - 1);
        }
    }

    void refresh() {
        es.execute(new ReadThread());


    }
}
